03 / 06

What is the difference between `null` and `undefined`?

  1. 1

    undefined is a value assigned to a variable that has been declared but hasn't been assigned a value.

  2. 2

    null is a value that represents the intentional absence of any object value.

undefined
  1. 1

    If a function doesn't have a return statement, it returns undefined by default.

  2. 2

    If you try to access an object property that doesn't exist, you get undefined

  3. 3

    undefined + 1 = NaN (Not a Number): You can't add 'nothingness' to a number.

Optional Chaining: The Optional Chaining operator allows you to read a property deep within a chain of connected objects without having to manually check if each level exists.

The Optional Chaining operator allows you to read a property deep within a chain of connected objects without having to manually check if each level exists. If the value before the ?. is null or undefined, the expression short-circuits and returns undefined instead of throwing a 'TypeError: Cannot read property of undefined.'
null
  1. 1

    It’s often used as a placeholder for an object that might be created later.

  2. 2

    It’s used to clear the contents of a variable or property.

  3. 3

    If you run typeof null, JavaScript returns 'object'.

  4. 4

    It is the root type in prototype chain

  5. 5

    In math, null is treated as 0.

Nullish Coalescing: The Nullish Coalescing operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined.

The || operator checks for falsy values. In JavaScript, 0, "" (empty string), and false are all falsy. Often, you want to keep 0 or "" as valid values, but || will skip them. ?? only cares about 'Nullish' values (null or undefined).
Difficulty: 3/10
Topics: type coercion, value initialization, error handling

Scenario Questions

0-2 years experience
  1. 1

    If you call a function that doesn't return anything and assign its result to a variable, what will that variable contain? How does that differ from a variable you explicitly set to null?

  2. 2

    When you read a property that doesn't exist on an object, what value do you get? What would you get if you had set that property to null instead?

  3. 3

    Write a short snippet that checks whether a variable is undefined versus null before using it.

2-5 years experience
  1. 1

    You receive a JSON payload where some fields may be missing, resulting in undefined, while others are explicitly null. How would you normalize these values before processing them?

  2. 2

    A function expects a string but receives undefined, causing a runtime error. Walk me through how you would debug this and decide whether to treat undefined as null.

  3. 3

    Explain why value == null matches both null and undefined, and when you would choose === instead.

5-8 years experience
  1. 1

    Design a utility that safely extracts nested properties without throwing errors, handling both undefined and null. What design choices would you make and why?

  2. 2

    When defining an API contract between front‑end and back‑end, how do you decide whether missing fields should be sent as undefined, null, or omitted? Discuss trade‑offs for type safety and serialization.

  3. 3

    During a TypeScript migration you want to tighten nullability. How would you refactor existing JavaScript that mixes undefined and null to avoid subtle bugs?

8+ years experience
  1. 1

    Your organization is moving to a polyglot microservices architecture. How would you establish a consistent convention for representing absent values across services, and what impact does the null vs undefined distinction have on data contracts, logging, and monitoring?

  2. 2

    You lead a team maintaining a legacy library used by many internal products, some of which check for undefined and others for null. How would you deprecate one pattern without breaking downstream consumers?

Follow-up Questions

  • When would you prefer `== null` over `=== null`?
  • What happens to an undefined property when you JSON.stringify an object?
  • How do TypeScript's strict null checks influence the use of null vs undefined?